Component({
  properties: {
    // 从父组件接收的数据
    productInformation: {
      type: Object,
      value: {}
    },
    selectedServiceConfigs: {
      type: Object,
      value: {}
    },
    serviceConfigs: {
      type: Array,
      value: []
    },
    address: {
      type: Object,
      value: {}
    },
    selectedTimeInfo: {
      type: Object,
      value: {}
    },
    availableTimeList: {
      type: Array,
      value: []
    },
    paoTuiTasks: {
      type: Array,
      value: []
    },
    paoTuiNote: {
      type: String,
      value: ''
    },
    paoTuiContactPhone: {
      type: String,
      value: ''
    }
  },

  data: {
    // 组件内部数据
  },

  methods: {
    // 选择跑腿服务配置（互斥选择，只能选择一个）
    selectPaoTuiServiceConfig(e) {
      const { serviceIndex, optionIndex } = e.currentTarget.dataset;
      const { serviceConfigs } = this.data;
      const selectedService = serviceConfigs[serviceIndex].options[optionIndex];
      const key = `${serviceIndex}_${optionIndex}`;
      
      // 如果已选中，则取消选择
      if (this.data.selectedServiceConfigs[key]) {
        this.triggerEvent('serviceConfigChange', {
          selectedServiceConfigs: {}
        });
      } else {
        // 如果未选中，则清空所有选择，只选中当前项并设置数量为1
        const newSelectedServiceConfigs = {
          [key]: {
            serviceIndex,
            optionIndex,
            selectedService,
            quantity: 1
          }
        };
        this.triggerEvent('serviceConfigChange', {
          selectedServiceConfigs: newSelectedServiceConfigs
        });
      }
    },

    // 跑腿服务数量调整（互斥选择模式）
    adjustPaoTuiServiceQuantity(e) {
      const { serviceIndex, optionIndex, action } = e.currentTarget.dataset;
      const key = `${serviceIndex}_${optionIndex}`;
      const currentConfig = this.data.selectedServiceConfigs[key];
      if (!currentConfig) return;
      
      let newQuantity = currentConfig.quantity || 1;
      if (action === 'increase') {
        newQuantity += 1;
      } else if (action === 'decrease') {
        newQuantity -= 1;
      }
      
      if (newQuantity <= 0) {
        // 当数量减少到0时，清空所有选择
        this.triggerEvent('serviceConfigChange', {
          selectedServiceConfigs: {}
        });
      } else {
        // 更新当前选择的数量
        const newSelectedServiceConfigs = {
          [key]: {
            ...currentConfig,
            quantity: newQuantity
          }
        };
        this.triggerEvent('serviceConfigChange', {
          selectedServiceConfigs: newSelectedServiceConfigs
        });
      }
    },

    // 手机号授权
    onLogin(e) {
      this.triggerEvent('login', e.detail);
    },

    // 跑腿备注输入
    onPaoTuiNoteInput(e) {
      this.triggerEvent('paoTuiNoteChange', { paoTuiNote: e.detail.value });
    },

    // 删除跑腿任务
    deletePaoTuiTask(e) {
      const { index } = e.currentTarget.dataset;
      this.triggerEvent('deletePaoTuiTask', { index });
    },

    // 跑腿任务图片上传
    async uploadPaoTuiTaskPhoto(e) {
      const { taskIndex } = e.currentTarget.dataset;
      this.triggerEvent('uploadPaoTuiTaskPhoto', { taskIndex });
    },

    // 预览跑腿任务图片
    previewPaoTuiTaskPhoto(e) {
      const { taskIndex, photoIndex } = e.currentTarget.dataset;
      this.triggerEvent('previewPaoTuiTaskPhoto', { taskIndex, photoIndex });
    },

    // 删除跑腿任务图片
    deletePaoTuiTaskPhoto(e) {
      const { taskIndex, photoIndex } = e.currentTarget.dataset;
      this.triggerEvent('deletePaoTuiTaskPhoto', { taskIndex, photoIndex });
    },

    // 快递代寄取货地址输入处理
    onPaoTuiTaskPickupAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'pickupAddressText', 
        value 
      });
    },

    // 快递代寄取货时间输入处理
    onPaoTuiTaskPickupTimeInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'pickupTimeText', 
        value 
      });
    },

    // 快递代寄送餐地址输入处理
    onPaoTuiTaskDeliveryAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'deliveryAddressText', 
        value 
      });
    },

    // 食堂取餐取餐地址输入处理
    onPaoTuiTaskCanteenPickupAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'pickupAddressText', 
        value 
      });
    },

    // 食堂取餐送餐地址输入处理
    onPaoTuiTaskCanteenDeliveryAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'deliveryAddressText', 
        value 
      });
    },

    // 代取外卖取餐地址输入处理
    onPaoTuiTaskTakeoutPickupAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'pickupAddressText', 
        value 
      });
    },

    // 代取外卖送餐地址输入处理
    onPaoTuiTaskTakeoutDeliveryAddressInput(e) {
      const index = e.currentTarget.dataset.index;
      const value = e.detail.value;
      this.triggerEvent('paoTuiTaskInputChange', { 
        index, 
        field: 'deliveryAddressText', 
        value 
      });
    },

    // 阻止事件冒泡
    preventBubble() {
      return;
    }
  }
}); 